package com.twang.stock.design.impl;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.design.IndexDataTask;
import com.twang.stock.dto.IndexedInfo;
import com.twang.stock.entity.CompanyStockMinutes;
import com.twang.stock.entity.TodayQinXu;
import com.twang.stock.entity.ZhangTingBan;
import com.twang.stock.entity.ex.HotTagStockInfo;
import com.twang.stock.mapper.CompanyStockMinutesMapper;
import com.twang.stock.mapper.TodayQinXuMapper;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.NotifyStockBuyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 大盘成交量
 */

@Service
@Slf4j
public class SumTotalTradeAmountTask extends IndexedInfo implements IndexDataTask {
    public static String cache_key = "SumTotalTradeAmountTask";

    public static String cache_key_szAndShTotalTradeSum="szAndShTotalTradeSum";

    public static String cache_key_LNQX = "cache_key_LNQX";

    public static String LNQX_1="放量 | 开心玩大成交额（这时候指标有效）";
    public static String LNQX_2="缩量(适当减仓) | 高抛低吸,高低切换快";
    public static String LNQX_3="严重缩量（空仓） | 小仓短线高度板！";

    public static String LNQX_4="市场成交活跃，上主流板块趋势品种！";

    public static String LNQX_5="跌停过多，空仓休息！";

    public static String cache_key_tradeDays="cache_key_tradeDays";
    public static String cache_key_szTradeAmount="cache_key_szTradeAmount";
    public static String cache_key_shTradeAmount="cache_key_shTradeAmount";
    public static String cache_key_totalTradeAmount="cache_key_totalTradeAmount";

    public static String cache_key_zhangTingBanList="zhangTingBanList";
    @Resource
    private TodayQinXuMapper todayQinXuMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private CompanyStockMinutesMapper companyStockMinutesMapper;
    @Autowired
    private NotifyStockBuyService notifyStockBuyService;



    @Override
    public void execute(ModelAndView modelAndView, CountDownLatch latch) throws Exception {
        try {
            List<String> days=todayQinXuMapper.queryDates();
            List<String> tradeDays= Lists.newArrayList();
            List<BigDecimal> szTradeAmount= Lists.newArrayList();
            List<BigDecimal> shTradeAmount= Lists.newArrayList();
            List<BigDecimal> totalTradeAmount= Lists.newArrayList();

            //
            modelAndView.addObject("LNQX", redisUtil.get(cache_key_LNQX));
            //
            Object obj = redisUtil.get(cache_key_szAndShTotalTradeSum);
            TodayQinXu todayQinXu= JSONObject.parseObject(obj.toString(), TodayQinXu.class);
            modelAndView.addObject("szAndShTotalTradeSum",todayQinXu);

            //
            Object cacheKeyTradeDays = redisUtil.get(cache_key_tradeDays);
             tradeDays= JSONObject.parseArray(cacheKeyTradeDays.toString(), String.class);
            modelAndView.addObject("tradeDays", tradeDays);

            //
            Object cacheKeyShTradeAmount = redisUtil.get(cache_key_shTradeAmount);
            shTradeAmount= JSONObject.parseArray(cacheKeyShTradeAmount.toString(), BigDecimal.class);
            modelAndView.addObject("shTradeAmount", shTradeAmount);
            //
            Object cacheKeySzTradeAmount = redisUtil.get(cache_key_szTradeAmount);
            szTradeAmount= JSONObject.parseArray(cacheKeySzTradeAmount.toString(), BigDecimal.class);
            modelAndView.addObject("szTradeAmount", szTradeAmount);
            //
            Object cacheKeyTotalTradeAmount = redisUtil.get(cache_key_totalTradeAmount);
            totalTradeAmount= JSONObject.parseArray(cacheKeyTotalTradeAmount.toString(), BigDecimal.class);
            modelAndView.addObject("totalTradeAMount", totalTradeAmount);
            //
            Object cacheKeyZhangTingBanList = redisUtil.get(cache_key_zhangTingBanList);
            List<ZhangTingBan>  zhangTingBanList= JSONObject.parseArray(cacheKeyZhangTingBanList.toString(), ZhangTingBan.class);
            //
            if(CollectionUtils.isNotEmpty(zhangTingBanList)){
                zhangTingBanList.stream().forEach(zhangTingBan -> {
                    String reason=notifyStockBuyService.getZtReason(zhangTingBan.getCode());
                    zhangTingBan.setZtReason(reason);
                });
            }

            modelAndView.addObject("zhangTingBanList", zhangTingBanList);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.info("大盘涨跌情况 执行完成，当前剩余条数:{}", latch.getCount());
            latch.countDown();
        }
    }

    private void  setZhangDie(List<HotTagStockInfo> list){
        list.stream().forEach(item->{
            CompanyStockMinutes companyStockMinutes =companyStockMinutesMapper.queryByCode(item.getCode());
            if(null!=companyStockMinutes){
                item.setZhangdie(companyStockMinutes.getZhangDie());
            }else{
                item.setZhangdie(BigDecimal.ZERO);
            }
        });
    }
}
